package com.fauks.ssm.mybatis;

import com.fauks.ssm.mybatis.bean.MCountry;
import com.fauks.ssm.mybatis.bean.MProvinceMany;
import com.fauks.ssm.mybatis.mapper.ManyToOneMapper;
import com.fauks.ssm.mybatis.mapper.OneToManyMapper;
import com.fauks.ssm.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 关联查询
 */
public class _04ConjunctiveQuery {

    private SqlSession sqlSession;
    private OneToManyMapper oneToManyMapper;
    private ManyToOneMapper manyToOneMapper;

    @Before
    public void initDao() {
        sqlSession = MyBatisUtils.getSqlSession();
        oneToManyMapper = sqlSession.getMapper(OneToManyMapper.class);
        manyToOneMapper = sqlSession.getMapper(ManyToOneMapper.class);
    }

    @After
    public void closeSession() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    @Test
    public void testOneToMany1() {
        MCountry countryById = oneToManyMapper.getCountryById(2);
        System.out.println(countryById);

        //直接从一级缓存获取(根据SQL语句的id即Dao的方法+SQL语句 )
        //当进行更新(增删改)操作时会刷新缓存即清空一级缓存
        MCountry countryById2 = oneToManyMapper.getCountryById(2);
        System.out.println(countryById2);
    }

    @Test
    public void testOneToMany2() {
        MCountry countryById = oneToManyMapper.getCountryById2(2);
        System.out.println(countryById.getCname());
    }

    @Test
    public void testManyToOne() {
        MProvinceMany provinceById = manyToOneMapper.getProvinceById(1);
        System.out.println(provinceById);
    }

    @Test
    public void testManyToOne2() {
        MProvinceMany provinceById = manyToOneMapper.getProvinceById2(2);
        System.out.println(provinceById);
    }

}
